/**
 * 
 */
package ar.com.ctrln.view.dashboard.mantenimiento.ticket;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Div;
import org.zkoss.zul.Groupbox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Window;

import ar.com.ctrln.control.mantenimiento.ticket.ConsultarTicket;
import ar.com.ctrln.control.mantenimiento.ticket.RegistrarTicket;
import ar.com.ctrln.persistence.model.ticket.TipoTicket;
import ar.com.ctrln.view.ui.comportamientoVentana.AbstractMinimizarVentana;

/**
 * @author Cristian
 * 
 */
@SuppressWarnings("unused")
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class FiltrarTicketsController extends AbstractMinimizarVentana {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Wire
	private Window wFiltrarTickets;

	private String estadoFiltro = "Pendiente";

	@WireVariable
	private ConsultarTicket consultarTicket;
	private Div btnFiltrar;

	@Wire
	private Datebox dbFechaHasta;
	@Wire
	private Datebox dbFechaDesde;

	private Groupbox gb;

	@Wire
	private Radio radioCerrado;

	@Wire
	private Radio radioPendiente;

	@Wire
	private Combobox cboTipoTicket;
	private Connection con;
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);

	}

	@Listen("onCreate = #cboTipoTicket")
	public void cargarTipoTicket() {
		ListModelList<TipoTicket> model = new ListModelList<TipoTicket>(
				consultarTicket.listaTipoTicket());
		TipoTicket tipoTodos = new TipoTicket();
		tipoTodos.setNombre("Todos");
		model.add(tipoTodos);
		cboTipoTicket.setModel(model);

	}

	@Listen("onCheck = #radioPendiente,#radioCerrado")
	public void filtrarEstado(Event event) {
		estadoFiltro = ((Radio) event.getTarget()).getValue();
	}

	@Listen("onClick = #btnFiltrar")
	public void generarReporte() throws SQLException, IOException {

		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/ctrln", "root", "root");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		 String path = this.getPage().getDesktop().getWebApp()
				    .getRealPath("/resources/reportes/ListarTicketsCerrados.jrxml");
		try {
			Map param = new HashMap();
			String tipo;
			tipo = cboTipoTicket.getText();
			if (tipo.equals("Todos") || cboTipoTicket.getText().equals("Seleccione ...")) {
				tipo = "('Mantenimiento','Instalacion','Desinstalacion','Reclamo')";
			} else {
				tipo = "('"
						+ ((TipoTicket) cboTipoTicket.getModel().getElementAt(
								cboTipoTicket.getSelectedIndex())).getNombre()
						+ "')";
			}
			param.put("varTipoTicket", tipo);
			param.put("varEstado", estadoFiltro);
			if (dbFechaDesde.getValue() == null) 
			{param.put("varDesde",	new SimpleDateFormat("yyyy-MM-dd").parse("1960-01-01"));
			} 
			else 
			{param.put("varDesde", (Date) dbFechaDesde.getValue());}

			if (dbFechaHasta.getValue() == null) 
			{param.put("varHasta", (Date) new Date());} 
			else 
			{param.put("varHasta", (Date) dbFechaHasta.getValue());}

			JasperReport jasperReport = JasperCompileManager.compileReport(path);
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, con);
			JasperViewer.viewReport(jasperPrint, false);
			con.close();
			wFiltrarTickets.onClose();
		} catch (JRException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}
}
